package com.tqk.constant;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.tqk.taxismart.obj.Taxi;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

public class Constant {
	private static final String TAG = "Constant";

	public static String readFileJson(Context context,String fileName){
		String datajson = null;
		try {
			InputStream is = context.getAssets().open(fileName);
			int size = is.available();
			byte[] buffer = new byte[size];
			is.read(buffer);
			is.close();
			datajson = new String(buffer,"UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return datajson;
	}
	
	public static void CallNumber(Context ctx,String number){
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		callIntent.setData(Uri.parse(number));
		ctx.startActivity(callIntent);
	}
	
	public static String DISTANCE = "";
	public static List<Taxi> DATA_TAXI = new ArrayList<Taxi>();
	
	public static void sortByPrice(List<Taxi> data){
		if (data.size() > 0) {
			Collections.sort(data, new Comparator<Taxi>() {

				@Override
				public int compare(Taxi t1, Taxi t2) {
					if (t1.getPrice2() == t2.getPrice2()) {
						return 0;
					}
					return t1.getPrice2() < t2.getPrice2()?-1:1;
				}

			});
		}
	}
	
	   public static List<Taxi> parserData(Context ctx){
	    	String dataJson = Constant.readFileJson(ctx, "data/list_taxi.json");
	    	
	    	try {
	    		JSONObject jsObj = new JSONObject(dataJson);
	    		if (jsObj != null) {
					JSONArray jsArray = jsObj.getJSONArray("data");
					Log.i(TAG, "leng data = "+jsArray.length());
					for (int i = 0; i < jsArray.length(); i++) {
						JSONObject obj = jsArray.getJSONObject(i);
						String name = obj.getString("name_taxi");
						String seatCount = obj.getString("seat_count");
						String number = obj.getString("number");
						float price1 = Float.valueOf(obj.getString("price1"));
						float price2 = Float.valueOf(obj.getString("price2"));
						float price3 = Float.valueOf(obj.getString("price3"));
						int rateCount = obj.getInt("rate");
						Taxi t = new Taxi();
						t.setNameTaxi(name);
						t.setseatCount(seatCount);
						t.setCallLine(number);
						t.setRateCount(rateCount);
						t.setPrice(price1);
						t.setPrice2(price2);
						t.setPrice3(price3);
						DATA_TAXI.add(t);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
	    	
	    	return DATA_TAXI;
	    }
}